package com.example.flowabledemo.config.flowable;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import java.io.IOException;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * @author linke
 * @date 2020/11/24 22:15
 * @description
 */
@Slf4j
@Configuration
public class FlowableConfig {

  //参数1为终端ID
  //参数2为数据中心ID
  private static Snowflake snowflake = IdUtil.getSnowflake(1, 1);

  @Autowired
  private DataSource dataSource;

  @Autowired
  private DataSourceTransactionManager transactionManager;

  @Bean
  public SpringProcessEngineConfiguration processEngineConfiguration() {
    SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
    configuration.setDataSource(dataSource);
    configuration.setTransactionManager(transactionManager);
    configuration.setDatabaseSchemaUpdate("true");
    configuration.setAsyncExecutorActivate(false);
    //采用雪花算法生成唯一id
    configuration.setIdGenerator(() -> snowflakeGenerateId());
   /*Resource resource = new ClassPathResource("processes/leaveProcesses.bpmn20.xml");
    Resource[] resources = new Resource[1];
    resources[0] = resource;*/
    configuration.setDeploymentResources(getProcessesBpm());
//    configuration.setDeploymentMode("resource-parent-folder");
    configuration.setDeploymentMode("default");
    // 解决流程图乱码
    configuration.setActivityFontName("宋体");
    configuration.setLabelFontName("宋体");
    configuration.setAnnotationFontName("宋体");
    return configuration;
  }

  @Bean
  public ProcessEngine processEngine() {
    return processEngineConfiguration().buildProcessEngine();
  }

  /**
   * 使用hutool提供的雪花算法生成唯一id
   */
  public static String snowflakeGenerateId() {
    return snowflake.nextIdStr();
  }

  /**
   * 手动加载配置，默认有时候不能加载
   */
  private Resource[] getProcessesBpm() {
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {
      Resource[] xml = resolver.getResources("classpath:processes/**.bpmn20.xml");
      Resource[] bpmn = resolver.getResources("classpath:processes/**.bpmn");
      int i = 0;
      Resource[] resources = new Resource[xml.length + bpmn.length];
      if (xml.length != 0) {
        for (Resource r : xml) {
          resources[i] = r;
          i++;
        }
      }
      if (bpmn.length != 0) {
        for (Resource r : bpmn) {
          resources[i] = r;
          i++;
        }
      }
      return resources;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return new Resource[0];
  }

  @Bean
  public RepositoryService repositoryService() {
    return processEngine().getRepositoryService();
  }

  @Bean
  public RuntimeService runtimeService() {
    return processEngine().getRuntimeService();
  }

  @Bean
  public TaskService taskService() {
    return processEngine().getTaskService();
  }
}
